//index.js
//获取应用实例
/*违章缴费-现场单*/
const util = require('../../utils/util')
const app = getApp()
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    // canIUse: wx.canIUse('button.open-type.getUserInfo'),
    date: new Date(),
    imageObject: {},
    violationNo: '',
    violationDate: '',
    violationFine: '',
    overdueFine: 0,
    serviceFee: 0,
    channel: '',
    sampleImg: '',
    appid: ''
  },
  onLoad: function (options) {
    console.log('index onLoad');
    if(options.channel || options.appid) {
      this.setData({
        channel: options.channel || '',
        appid: options.appid || app.globalData.appid || ''
      })
    }
    
    // swan.showShareMenu({//设置转发分享按钮
    //   withShareTicket: true
    // })
    
    loadBanner(this);
    /*
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true,
        channel: options.channel || ''
      })
      // updateUserInfo()
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
          channel: options.channel || ''
        })
        // updateUserInfo()
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true,
            channel: options.channel || ''
          })
          // updateUserInfo()
        }
      })
    }*/
  },
  getUserInfo: function (e) {
    // console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
    // updateUserInfo()
  },
  showImg: function(e) {
    /*
    const sample = 'http://wz.whodear.com/h5/image/weiz.jpg'
    wx.previewImage({
      current: sample,     //当前图片地址
      urls: [sample],               //所有要预览的图片的地址集合 数组形式
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
    */
  },
  
  bindPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      violationDate: e.detail.value,
    });
    // console.log('this.violationDate', this.data.violationDate);
    this.computeAmout();
  },
  viewExampleImg: function(e) {
    const sample = this.data.sampleImg;
    swan.previewImage({
      current: sample,     //当前图片地址
      urls: [sample],               //所有要预览的图片的地址集合 数组形式
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  gotoSubmit: function () {
    if(!this.data.violationNo || !(this.data.violationNo.length <= 16 && this.data.violationNo.length >= 15)) {
      swan.showToast(
        {
          icon: 'none',
          title: '请正确填写罚单编号',
          duration: 1200
        }
      );
      return;
    }
    if(!this.data.violationDate) {
      swan.showToast(
        {
          icon: 'none',
          title: '请选择处罚日期',
          duration: 1500
        }
      );
      return;
    }
    if(!this.data.violationFine) {
      swan.showToast(
        {
          icon: 'none',
          title: '请输入处罚金额',
          duration: 1500
        }
      );
      return;
    }
    if(!app.globalData.config) {
      return;
    }

    app.globalData.order = {
      violationNo: this.data.violationNo,
      violationDate: this.data.violationDate,
      violationFine: this.data.violationFine,
      overdueFine: this.data.overdueFine
    };

    swan.navigateTo({
      url: `../submit/submit?channel=${this.data.channel}&appid=${this.data.appid}`
    })
  },
  clickQuestion: function () {
    swan.switchTab({
      url: '../contact/contact'
    })
  },
  didPressChooesImage: function () {
    var that = this;
    didPressChooesImage(that);
  },
  didCancelTask: function () {
    this.data.cancelTask()
  },
  inputViolationNo: function(e) {
    this.data.violationNo = e.detail.value;
    this.computeAmout();
    return this.violationNo;
  },
  inputViolationFine: function(e) {
    this.data.violationFine = e.detail.value;
    this.computeAmout();
    return this.data.violationFine;
  },
  computeAmout: function() {
    this.setData({
        serviceFee: 0,
        overdueFine: 0
    });
    if(!this.data.violationNo || !(this.data.violationNo.length <= 16 && this.data.violationNo.length >= 15)) {
      return;
    }

    if(!this.data.violationDate) {
      return;
    }
    
    if(!this.data.violationFine) {
      return;
    }
    updateData(this);
  }
});

function updateData(that) {
  let config = app.globalData.config;
  if(config) {
      let days = util.getDays(that.data.violationDate, util.getStrDate()) + 1;
      let overdueFine = 0;
      if(days > config.overday) {
          overdueFine = (days - config.overday) * config.overdue * that.data.violationFine;
          if(overdueFine > that.data.violationFine) {
              overdueFine = that.data.violationFine;
          }
      }
      let amout = Number(that.data.violationFine) + Number(overdueFine) + (Number(config.serviceFee));
      that.setData({
          serviceFee: config.serviceFee,
          overdueFine: overdueFine
      });
  }
}

function loadBanner(that) {
  const url = `${app.globalData.prefix}/api/banner`;
  swan.request({
    url: url,
    method: 'GET',
    header: {
        channel: that.data.channel || '',
        appid: that.data.appid || ''
    },
    success: function(ret) {
        console.log(url);
        if(!ret.data.status) {
            
        }
        else {
            that.setData({
              banner: ret.data.data.img,
              imgHeight: ret.data.data.height,
              sampleImg: ret.data.data.sampleImg
            });
        }
    },
    fail: function(ret) {
        console.log('get banner error', ret);
    }
  });
}
